<template>
  <div class='container'>
<el-form :inline="true" :model="form" class="demo-form-inline">
  <div v-if="$route.query.uname">
      <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item >学科管理</el-breadcrumb-item>
  <el-breadcrumb-item>{{$route.query.uname}}</el-breadcrumb-item>
  <el-breadcrumb-item>标签管理</el-breadcrumb-item>
</el-breadcrumb>
<el-divider></el-divider>
    </div>
    <div class="users_main_head">
        <el-form-item  label="标签名称">
          <el-input
            class="users_main_head_search"
            v-model="form.tagName"
          ></el-input>
        </el-form-item>
          <el-form-item label="状态" >
    <el-select v-model="form.state" placeholder="请选择">
      <el-option label="启用" value="1"></el-option>
      <el-option label="禁用" value="0"></el-option>
    </el-select>
  </el-form-item>

        <el-form-item>
          <el-button @click="resetForm">清空</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
          <i v-if="$route.query.uname" class="el-icon-back" @click="$router.push('list')">返回学科</i>
          <el-button
            type="success"
            :class="{newElement:isShow}"
            @click="openDrawer('add')"
            >新增标签</el-button
          >
        </el-form-item>
      </div>
      <div class="uesrs_main_statistics">共{{total}}条记录</div>
      <el-table ref="table" :data="randerList" style="width: 100%">
        <el-table-column prop="id" label="序号" width="90"> </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="200">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称" width="220">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="200">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="210">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="170">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{row }">
            <el-button type="text" v-if="row.state!=='已启用'"  @click="isState(row.id,row.state)" >启用</el-button>
            <el-button type="text" v-else  @click="isState(row.id,row.state)">禁用</el-button>
            <el-button type="text" :disabled="row.state==='已禁用'"  @click="openDrawer('edit',row.id)">修改</el-button>
            <el-button type="text" :disabled="row.state==='已禁用'" @click="delDrawer(row.id)" >删除</el-button>

          </template>
        </el-table-column>
      </el-table>
       <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="total"
      >
      </el-pagination>
      </el-form>
      <TagsAdd @newDataes='newDataes' :isShow='isShow' :formBase='formBase'  :dialogFormVisible.sync='dialogFormVisible'></TagsAdd>
  </div>
</template>

<script>
import moment from 'moment'
import TagsAdd from '@/module-hmmm/components/tags-add'
import { list, remove, changeState } from '@/api/hmmm/tags.js'
export default {
  components: {
    TagsAdd
  },
  data () {
    return {
      isShow: true,
      dialogFormVisible: false,
      formBase: {
        subjectName: '',
        tagName: '',
        subjectID: ''
      },
      form: {
        tagName: '',
        state: null
      },
      query: {
        page: 1,
        pagesiae: 10
      },
      total: 0,
      randerList: []
    }
  },
  async created () {
    this.initData()
  },
  methods: {
    //  获取数据渲染页面
    async initData () {
      if (this.$route.query.id) {
        this.isShow = false
        const { data } = await list({ subjectID: this.$route.query.id })
        this.total = data.counts
        this.randerList = data.items
        this.randerList.forEach(item => {
          item.state = item.state ? '已启用' : '已禁用'
          item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
        })
      } else {
        const { data } = await list(this.query)
        this.total = data.counts
        this.randerList = data.items
        this.randerList.forEach(item => {
          item.state = item.state ? '已启用' : '已禁用'
          item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
        })
      }
    },
    // 点击搜索发生什么
    async onSubmit () {
      console.log(this.form.state)
      // const state = this.form.state === '启用' || '禁用' ? (this.form.state === '启用' ? 1 : 0) : null
      const { data } = await list(this.form)
      this.randerList = data.items
      this.total = data.counts
      this.randerList.forEach(item => {
        item.state = item.state ? '已启用' : '已禁用'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 点击清空触发事件
    resetForm () {
      this.form.tagName = ''
      this.form.state = ''
      this.initData()
    },
    // 点击添加或者修改
    openDrawer (type, id) {
      this.dialogFormVisible = true
      if (type !== 'add') {
        this.randerList.forEach(item => {
          if (item.id === id) {
            this.formBase.id = id
            this.formBase.tagName = item.tagName
            this.formBase.subjectID = item.subjectID
            this.formBase.subjectName = item.subjectName
          }
        })
      } else {
        if (this.$route.query.id) {
          this.formBase.id = ''
          this.formBase.directoryName = ''
          this.formBase.subjectID = +this.$route.query.id
        } else {
          this.formBase.id = ''
          this.formBase.directoryName = ''
          this.formBase.subjectID = ''
        }
      }
    },
    async isState (id, state) {
      const obj = { id, state }
      if (obj.state === '已启用') {
        obj.state = 0
      } else {
        obj.state = 1
      }
      await changeState(obj)
      this.initData()
    },
    async delDrawer (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async newDataes (data) {
      this.initData()
    },
    // 分页点击
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.initData()
    }
  }
}
</script>

<style scoped lang='less'>
// 表单
.demo-form-inline {
  width: 1300px;
  padding:  20px;
  background-color: #fff;
  margin: 20px;
  .users_main_head {
    width: 100%;
    padding: 20px 0;
  }
}
/deep/.users_main_head_search {
  width: 200px !important;
  height: 32px;
}
// 新增按钮
.newElement {
  margin-left: 450px;
}
.uesrs_main_statistics {
  font-size: 13px;
  background-color: #f4f4f5;
  color: #909399;
  padding: 10px 20px;
}
a{
  margin-right: 8px;
  color: #409eff;
}
.el-pagination{
  margin-left: 610px;
  margin-top: 20px;
}
.operation{
  border: none;
  width: 30px;
  height: 35px;
  background-color: rgba(red, green, blue, 1);
  color: #c0c4cc;
}
.isOperation{
border: none;
  width: 30px;
  height: 35px;
  background-color: rgba(red, green, blue, 1);
  color: #66b1ff;
}
i{
  color: #66b1ff;
  margin-left: 366px;
  margin-right: 10px;
}
</style>
